package com.lun.blog.controller.web;

import com.lun.blog.pojo.Message;
import com.lun.blog.pojo.User;
import com.lun.blog.service.CommentService;
import com.lun.blog.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author fangLJ
 * @creatTime 2021/3/7
 */
@Controller
@RequestMapping("/web/message")
public class MessageController {

    @Autowired
    MessageService messageService;

    @GetMapping("/toMessage")
    public String toMessage(Model model){
        model.addAttribute("messages",messageService.findAll());
        return "message";
    }

    @GetMapping("/messageComment")
    public String messageComment(Model model){
        model.addAttribute("messages",messageService.findAll());
        return "message :: messageList";
    }

    @PostMapping("/addMessage")
    public String addMessage(Message message, HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user != null){
            message.setAdminMessage(true);
            message.setAvatar(user.getAvatar());
        }else {
            message.setAvatar("/images/avatar.png");
            message.setAdminMessage(false);
        }
        messageService.addMessage(message);
        return "redirect:/web/message/messageComment";
    }

}
